//
//  HJSetPayPasswordController.m
//  KaiChat
//
//  Created by lx on 2024/1/31.
//

#import "HJPaySetPasswordController.h"
#import "HJTitleTextField.h"
#import "HJErrorTipView.h"
#import "QMUIButton.h"
#import "HJMinePresent.h"

@interface HJPaySetPasswordController ()

@property (nonatomic, weak) HJErrorTipView *tipView;
@property (nonatomic, weak) HJTitleTextField *passwordTextField;
@property (nonatomic, weak) HJTitleTextField *comfirmPasswordTextField;
@property (nonatomic, assign) BOOL isAgreement;
@property (nonatomic, weak) HJStyle1Button *confirmButton;
@property (nonatomic, assign) CGFloat tipViewH;

@end

@implementation HJPaySetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
        
    UIScrollView *scrollView = [UIScrollView addToView:self.view];
    scrollView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, KScreenHeight - KNavBarHight);
    scrollView.backgroundColor = UIColor.tj_backgroundColor;
    CGSize contentSize = scrollView.bounds.size;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    scrollView.contentSize = contentSize;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
 
    UIView *contentView = [[UIView alloc] init].addTo(scrollView);
    contentView.backgroundColor = UIColor.tj_backgroundColor;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.width.offset(contentSize.width);
        make.height.offset(contentSize.height);
    }];
    
    UILabel *textLabel = [UILabel lableWithSuperView:contentView font:UIFontMediumMake(20) color:UIColor.tj_333333Color title:@"Create Kaipay Password" textAlignment:NSTextAlignmentLeft];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.left.offset(leftRightMargin);
    }];
    
    UILabel *subTextLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(14) color:UIColor.tj_868391Color title:@"Please note that this password is different fromtheaccount password and only serves as aconfirmationpassword for Kaipay transactions" textAlignment:NSTextAlignmentLeft];
    subTextLabel.numberOfLines = 0;
    subTextLabel.attributedText = [subTextLabel.text titleMargin:1 alignment:NSTextAlignmentLeft];
    [subTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(textSubTextLabelMargin);
        make.left.right.inset(leftRightMargin);
    }];
    
    HJTitleTextField *passwordTextField = [HJTitleTextField addToView:contentView];
    passwordTextField.backgroundColor = UIColor.whiteColor;
    passwordTextField.titleLabel.text = @"Set Password";
    passwordTextField.textField.placeholder = @"6 Digits";
    passwordTextField.textField.maximumTextLength = 6;
    passwordTextField.secureTextEntry = YES;
    passwordTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
    passwordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [passwordTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(subTextLabel.mas_bottom).offset(40);
        make.height.offset(72);
    }];
//    
//    HJErrorTipView *tipView = [[HJErrorTipView alloc] initWithText:@"6 Digits" showCloseButton:YES];
//    [tipView.closeButton addTarget:self action:@selector(didClickCloseButton) forControlEvents:UIControlEventTouchUpInside];
//    [contentView addSubview:tipView];
//    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(passwordTextField.mas_bottom).offset(10);
//        make.left.right.equalTo(passwordTextField);
//    }];
//    
    HJTitleTextField *comfirmPasswordTextField = [HJTitleTextField addToView:contentView];
    comfirmPasswordTextField.backgroundColor = UIColor.whiteColor;
    comfirmPasswordTextField.titleLabel.text = @"Confirm Password";
    comfirmPasswordTextField.textField.placeholder = @"6 Digits";
    comfirmPasswordTextField.secureTextEntry = YES;
    comfirmPasswordTextField.textField.maximumTextLength = 6;
    comfirmPasswordTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
    comfirmPasswordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [comfirmPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(passwordTextField.mas_bottom).offset(20);
        make.height.equalTo(passwordTextField);
    }];
    
    HJStyle1Button *confirmButton = [HJStyle1Button buttonWithSuperView:contentView font:UIFontMediumMake(14) color:nil title:@"Confirm"];
    confirmButton.enabled = NO;
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(buttonHeight);
        make.top.equalTo(comfirmPasswordTextField.mas_bottom).offset(30);
    }];

    _confirmButton = confirmButton;
//    _tipView = tipView;
    _passwordTextField = passwordTextField;
    _comfirmPasswordTextField = comfirmPasswordTextField;
    
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
//    _tipViewH = ceil(tipView.tj_height);
}

- (void)textFieldDidChange:(UITextField *)textField {
    if ([HJTools verifyPayPassword:textField.text]) {
//        [self showTip:NO];
        _confirmButton.enabled = YES;
    } else {
        _confirmButton.enabled = NO;
//        [self showTip:YES];
    }

}

- (void)didClickCloseButton {
    [self showTip:NO];
}
 
- (void)showTip:(BOOL)show {
    [UIView animateWithDuration:0.25 animations:^{
        if (show) {
            [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(self.tipViewH);
            }];
            [self.comfirmPasswordTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.inset(leftRightMargin);
                make.top.equalTo(self.tipView.mas_bottom).offset(20);
                make.height.offset(72);
            }];
        } else {
            [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
            [self.comfirmPasswordTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.inset(leftRightMargin);
                make.top.equalTo(self.passwordTextField.mas_bottom).offset(20);
                make.height.offset(72);
            }];
        }
        
        [self.comfirmPasswordTextField.superview setNeedsLayout];
        [self.comfirmPasswordTextField.superview layoutIfNeeded];
    }];
}


- (void)didClickConfirmButton {

    NSString *password = _passwordTextField.textField.text;
    NSString *comfirmPassword = _comfirmPasswordTextField.textField.text;
    
    if (![password isEqualToString:comfirmPassword]) {
        [HJHUD showErrorMessage:@"Passwords entered twice are not the same"];
        return;
    }
    
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent setPayPasswordWithEmail:self.email code:self.emailCode password:password complete:^(NSError * _Nullable error) {
        if (!error) {
            if (self.setComplete) {
                self.setComplete();
            }
            self.navigationController.viewControllers = [self removeNavChildViewControllers:@[@"HJPaySendCodeController"]];
            [self.navigationController popViewControllerAnimated:YES];
            return;
        }
    }];
}

 

@end
